/* =========================================================================
 *
 * render.js
 *  This script contains the game logic acts as a controller for the Entity 
 *  Component System
 *
 * ========================================================================= */
function clearCanvas () {
    // Store the current transformation matrix
    ECS.context.save();

    // Use the identity matrix while clearing the canvas
    ECS.context.setTransform(1, 0, 0, 1, 0, 0);
    ECS.context.clearRect(0, 0, ECS.$canvas.width, ECS.$canvas.height);

    // Restore the transform
    ECS.context.restore();
}


// ECS - System - Render
// --------------------------------------
ECS.systems.render = function systemRender ( entities ) {

    // This happens each tick, so we need to clear out the previous rendered state
    clearCanvas();

    var curEntity; 

    // iterate over all entities
    for( var entityId in entities ){
        curEntity = entities[entityId];

        // Only run logic if entity has relevant components
        if( curEntity.components.Image && curEntity.components.Position ){
        	ECS.context.drawImage(curEntity.components.Image.image, curEntity.components.Position.x, curEntity.components.Position.y);
        }
    }
};